/**************************************************************

Copyright Astrium Services GEO-Information / Spot Image
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

 ***************************************************************/
package com.spotimage.catalog.client.ui.listgrid;

import com.smartgwt.client.data.Record;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;
import com.smartgwt.client.widgets.menu.MenuItem;
import com.smartgwt.client.widgets.menu.events.ClickHandler;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;
import com.spotimage.catalog.client.event.DeselectedSearchResultEvent;
import com.spotimage.catalog.client.event.MoveToSelectionEvent;
import com.spotimage.catalog.client.event.EventBus;
import com.spotimage.catalog.client.event.RemoveFromSearchResultEvent;
import com.spotimage.catalog.client.event.SelectedSearchResultEvent;
import com.spotimage.catalog.client.util.Util;

/**
 * Search result scene items
 */
public class SearcResultListGrid extends SceneListGrid {

	/**
	 * Set context menu.
	 */
	public SearcResultListGrid() {
		super(false);
		
		// context menu
		MenuItem addToSelection = new MenuItem("Add to selection");
		addToSelection.setIcon(Util.getImageUrl("cart-add.png"));
		addToSelection.addClickHandler(new ClickHandler() {
			//@Override
			public void onClick(MenuItemClickEvent event) {
				SceneListGridRecord selectedRecord = (SceneListGridRecord) getSelectedRecord();
				removeData(selectedRecord);
				EventBus.getInstance().fireEvent(new MoveToSelectionEvent(selectedRecord));
			}
		});
		contextMenu.addItem(addToSelection, 0);
		MenuItem remove = new MenuItem("Remove");
		remove.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {
			//@Override
			public void onClick(MenuItemClickEvent event) {
				SceneListGridRecord selectedRecord = (SceneListGridRecord) getSelectedRecord();
				removeData(selectedRecord);
				EventBus.getInstance().fireEvent(new RemoveFromSearchResultEvent(selectedRecord));
			}
		});
		contextMenu.addItem(remove);
		MenuItem removeAll = new MenuItem("Remove all");
		removeAll.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {
			//@Override
			public void onClick(MenuItemClickEvent event) {
				while (getRecordList().getLength() > 0) {
					Record record = getRecordList().get(0);
					removeData(record);
					EventBus.getInstance().fireEvent(new RemoveFromSearchResultEvent((SceneListGridRecord) record));
				}
			}
		});
		contextMenu.addItem(removeAll);
		
		// handlers
		addSelectionChangedHandler(new SelectionChangedHandler() {
			//@Override
			public void onSelectionChanged(SelectionEvent event) {
				SceneListGridRecord sceneListGridRecord = (SceneListGridRecord) event.getRecord();
				if (!event.getState()) {
					// item is deselected
					EventBus.getInstance().fireEvent(new DeselectedSearchResultEvent(sceneListGridRecord.getId()));
				} else {
					// item is selected
					EventBus.getInstance().fireEvent(new SelectedSearchResultEvent(sceneListGridRecord.getId()));
				}
			}
		});
	}
	
}
